

#pragma once
#include <wingdi.h>
#include<stdio.h>

class BitmapFont
{
public:
	GLuint	mBase;				// Base Display List For The Font Set
	GLfloat	cnt1;				// 1st Counter Used To Move Text & For Coloring
	GLfloat	cnt2;				// 2nd Counter Used To Move Text & For Coloring
	HDC mHdc;
	BitmapFont(HDC);
	GLvoid BuildFont(GLvoid);
	GLvoid KillFont(GLvoid);									// Delete The Font List
	GLvoid Print(float,float,const char *fmt,...);
	GLvoid SetColor(float,float,float);
};


/*
glColor3f(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2)));
	// Position The Text On The Screen
	glRasterPos2f(-0.45f+0.05f*float(cos(cnt1)), 0.32f*float(sin(cnt2)));
 	glPrint("Active OpenGL Text With NeHe - %7.2f", cnt1);	// Print GL Text To The Screen

*/
GLvoid BitmapFont::SetColor(float r, float g, float b)
{
	glColor3f(r,g,b);
}

GLvoid BitmapFont::Print(float x, float y,const char *fmt, ...)					// Custom GL "Print" Routine
{
	char		text[256];								// Holds Our String
	va_list		ap;										// Pointer To List Of Arguments

	if (fmt == NULL)									// If There's No Text
		return;											// Do Nothing

	glRasterPos2f(x,y);
	va_start(ap, fmt);									// Parses The String For Variables
	    vsprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers
	va_end(ap);											// Results Are Stored In Text

	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
	glListBase(mBase - 32);								// Sets The Base Character to 32
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	glPopAttrib();										// Pops The Display List Bits
}


GLvoid BitmapFont::KillFont(GLvoid)
{
	glDeleteLists(mBase, 96);							// Delete All 96 Characters
}

BitmapFont::BitmapFont(HDC inHdc)
{
	mHdc = inHdc;
}

GLvoid BitmapFont::BuildFont(GLvoid)
{
	HFONT oldFont;
	HFONT font;

	mBase = glGenLists(96);
	
	font = CreateFont(	-14,							// Height Of Font
						0,								// Width Of Font
						0,								// Angle Of Escapement
						0,								// Orientation Angle
						FW_BOLD,						// Font Weight
						FALSE,							// Italic
						FALSE,							// Underline
						FALSE,							// Strikeout
						ANSI_CHARSET,					// Character Set Identifier
						OUT_TT_PRECIS,					// Output Precision
						CLIP_DEFAULT_PRECIS,			// Clipping Precision
						ANTIALIASED_QUALITY,			// Output Quality
						FF_DONTCARE|DEFAULT_PITCH,		// Family And Pitch
						"Courier New");					// Font Name

	oldFont = (HFONT) SelectObject(mHdc,font);
	wglUseFontBitmaps(mHdc, 32, 96, mBase);				// Builds 96 Characters Starting At Character 32
	SelectObject(mHdc, oldFont);							// Selects The Font We Want
	DeleteObject(font);									// Delete The Font
	
}